package com.cqc.googleplay.fragment.home;

import java.util.ArrayList;
import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.adapter.StellarMapAdapter;
import com.cqc.googleplay.http.HttpHelper;
import com.cqc.googleplay.http.NetUrl;
import com.cqc.googleplay.utils.CommonUtil;
import com.cqc.googleplay.utils.GsonUtil;
import com.cqc.googleplay.utils.LogUtil;
import com.cqc.googleplay.view.ContentPager;
import com.cqc.googleplay.view.randomlayout.StellarMap;
import com.google.gson.reflect.TypeToken;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RecommendFrag extends Fragment {
	
	private static final String TAG = "RecommendFrag";
	private ContentPager contentPager;
	private List<String> list = new ArrayList<String>();
	private StellarMapAdapter adapter;
	private int padding = 10;
	private StellarMap map;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentPager = new ContentPager(getActivity()) {
			
			@Override
			public Object requestData() {
				return getData();
			}
			
			@Override
			public View createSuccessView() {
				return getSuccessView();
			}
		};
		return contentPager;
	}

	protected View getSuccessView() {
		int padding = (int) CommonUtil.getDimension(R.dimen.recommend_frag_padding);
		map = new StellarMap(getActivity());
		map.setInnerPadding(padding, padding, padding, padding);

		return map;
	}

	protected Object getData() {
		String json = HttpHelper.get(NetUrl.RECOMMEMD);
		List textList = (List<String>) GsonUtil.jsonToList(json, new TypeToken<List<String>>() {}.getType());
		list.addAll(textList);
		CommonUtil.runOnUIThread(new Runnable() {
			
			@Override
			public void run() {
				adapter = new  StellarMapAdapter(list, getActivity());	
				map.setAdapter(adapter);
				map.setGroup(0, true); 
				map.setRegularity(15, 15); 
			}
		});
		return json;
	}
}
